package com.huatao.subject.application.controller;

import com.google.common.base.Preconditions;
import com.huatao.subject.application.convert.SubjectAnswerConvert;
import com.huatao.subject.application.convert.SubjectInfoConvert;
import com.huatao.subject.application.dto.SubjectInfoDTO;
import com.huatao.subject.common.entity.PageResult;
import com.huatao.subject.common.entity.Result;
import com.huatao.subject.domain.entity.SubjectAnswerBO;
import com.huatao.subject.domain.entity.SubjectInfoBO;
import com.huatao.subject.domain.service.SubjectInfoDomainService;
import com.huatao.subject.infra.basic.entity.SubjectInfoEs;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/subject")
@Slf4j
public class SubjectController {

    @Resource
    private SubjectInfoDomainService subjectInfoDomainService;

    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController add subjectInfoDTO: {}", subjectInfoDTO);
            }
            Preconditions.checkArgument(!StringUtils.isBlank(subjectInfoDTO.getSubjectName()), "题目名称不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectScore(), "题目分数不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectType(), "题目类型不能为空");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoDTO.getCategoryIds()), "分类id不能为空");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoDTO.getLabelIds()), "标签id不能为空");
            SubjectInfoBO subjectInfoBO = SubjectInfoConvert.INSTANCE.convertSubjectInfoDtoToBo(subjectInfoDTO);
            List<SubjectAnswerBO> subjectAnswerBOList = SubjectAnswerConvert
                    .INSTANCE.convertSubjectAnswerDtoListToBo(subjectInfoDTO.getOptionLists());
            subjectInfoBO.setOptionLists(subjectAnswerBOList);
            subjectInfoDomainService.add(subjectInfoBO);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("SubjectController add error: {}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    @PostMapping("/getSubjectPage")
    public Result<PageResult<SubjectInfoDTO>> getSubjectPage(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController getSubjectPage subjectInfoDTO: {}", subjectInfoDTO);
            }
            Preconditions.checkNotNull(subjectInfoDTO.getCategoryId(), "分类id不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getLabelId(), "标签id不能为空");
            SubjectInfoBO subjectInfoBO = SubjectInfoConvert.INSTANCE.convertSubjectInfoDtoToBo(subjectInfoDTO);
            PageResult<SubjectInfoBO> boList = subjectInfoDomainService.getSubjectPage(subjectInfoBO);
            return Result.ok(boList);
        } catch (Exception e) {
            log.error("SubjectController getSubjectPage error: {}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    @PostMapping("/getSubjectPageBySearch")
    public Result<PageResult<SubjectInfoEs>> getSubjectPageBySearch(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController getSubjectPageBySearch subjectInfoDTO: {}", subjectInfoDTO);
            }
            Preconditions.checkArgument(StringUtils.isNotBlank(subjectInfoDTO.getKeyWords()), "keyWord不能为空");
            SubjectInfoBO subjectInfoBO = SubjectInfoConvert.INSTANCE.convertSubjectInfoDtoToBo(subjectInfoDTO);
            subjectInfoBO.setPageNo(subjectInfoDTO.getPageNo());
            subjectInfoBO.setPageSize(subjectInfoDTO.getPageSize());
            PageResult<SubjectInfoEs> esList = subjectInfoDomainService.getSubjectPageBySearch(subjectInfoBO);
            return Result.ok(esList);
        } catch (Exception e) {
            log.error("SubjectController getSubjectPageBySearch error: {}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    @PostMapping("/querySubjectInfo")
    public Result<SubjectInfoDTO> querySubjectInfo(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController querySubjectInfo subjectInfoDTO: {}", subjectInfoDTO);
            }
            Preconditions.checkNotNull(subjectInfoDTO.getId(), "id不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getCategoryId(), "分类id不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getLabelId(), "标签id不能为空");
            SubjectInfoBO subjectInfoBO = SubjectInfoConvert.INSTANCE.convertSubjectInfoDtoToBo(subjectInfoDTO);
            SubjectInfoBO bo = subjectInfoDomainService.querySubjectInfo(subjectInfoBO);
            SubjectInfoDTO dtoRes = SubjectInfoConvert.INSTANCE.convertSubjectInfoBoToDto(bo);
            return Result.ok(dtoRes);
        } catch (Exception e) {
            log.error("SubjectController querySubjectInfo error: {}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }
}
